<?php 
include("include/config.php");
	
if(@$_POST['limba']){
	switch($_POST['limba']){
		case "ro":
			$_SESSION['limba'] = 'ro';
		break;
		case "en":
			$_SESSION['limba'] = 'en';
		break;
		default:
			$_SESSION['limba'] = 'ro';
		break;
	}
}



if(@$_GET['actiune']){
		switch($_GET['actiune']){
			case "add":
				if(@$_POST){
				$lat = mysql_real_escape_string($_POST['lat']);
				$lon	= mysql_real_escape_string($_POST['lon']);
				$magnitude = mysql_real_escape_string($_POST['magnitude']);
				$time	= mysql_real_escape_string($_POST['time']);
				$region	= mysql_real_escape_string($_POST['region']);
				$review  =  mysql_real_escape_string($_POST['review'] );
				$depth =  mysql_real_escape_string($_POST['depth']);
				
				if(@$_POST['software']){
					$tipSoftware = mysql_real_escape_string($_POST['software']);
					if($tipSoftware == "ews"){
						$ordineSoftware = "3";
					}else{
						$ordineSoftware = "1";
					}
				}else{
					$tipSoftware = "antelope";
					$ordineSoftware = "2";
				}
				$radius = "111";  //Kilometri
				
				switch($review){
					case "y":
						$tip = "M";
					break;
					case "Y":
						$tip = "M";
					break;
					case "1":
						$tip = "M";
					break;
					case "m":
						$tip = "M";
					break;
					case "M":
						$tip = "M";
					break;
					default:
						$tip = "A";
					break;
				}
				$data_ora =  date("Y-m-d H:i:s",strtotime($time) - 10900); 

				$res = mysql_query("SELECT *, ((ACOS(SIN('$lat' * PI() / 180) * SIN(lat * PI() / 180) + COS('$lat' * PI() / 180) * COS(lat * PI() / 180) * COS(('$lon' - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `evenimente` HAVING distance <= '$radius' and data_eveniment BETWEEN DATE_ADD(now(), INTERVAL -30 SECOND) AND DATE_ADD(now(), INTERVAL 30 SECOND) ORDER BY distance ASC") or die(mysql_error());
					$arrID = array();
					$mysqlErrArr = array();
				if(mysql_num_rows($res)==0){
					mysql_query("insert into evenimente (id_cutremur,data_eveniment,mag,adancime,lat,lon,regiune,tip,tipSoftware,ordineSoftware) VALUES (null,'$data_ora','$magnitude','$depth','$lat','$lon','$region','$tip','$tipSoftware','$ordineSoftware')");
					$arrID[] = mysql_insert_id();
					$mysqlErrArr[] = mysql_error();


					#$dataArr = array("lat"=>$lat,"lon"=>$lon,"magnitude"=>$magnitude,"time"=>$time,"region"=>$region,"review"=>$review,"depth"=>$depth);

				}else{
					while($row = mysql_fetch_array($res)){
						$id_eveniment = $row['id_cutremur'];
						mysql_query("UPDATE evenimente set
							data_eveniment = '$data_ora',
							mag = '$magnitude',
							adancime = '$depth',
							lat = '$lat',
							lon ='$lon',
							regiune = '$region',
							tip = '$tip',
							tipSoftware = '$tipSoftware',
							ordineSoftware = '$ordineSoftware',
							where id_cutremur = '$id_eveniment'
						");
						$arrID[] = $id_eveniment;
						$mysqlErrArr[] = mysql_error();
					
					}
				}

	

				if(empty($mysqlErrArr)){
					header('HTTP/1.1 200 OK', true, 200);
					$arr['succes'] = '0';
					$arr['error'] = $mysqlErrArr;
					echo json_encode($arr);
				}else{
					header('HTTP/1.1 201 Created', true, 201);
					$arr = array();
					$arr['succes'] = '1';
					$arr['id'] = $arrID;
					echo json_encode($arr);
				}
			}else{
					$mysqlErrArr = "Nu s au primit date!";
					header('HTTP/1.1 200 OK', true, 200);
					$arr['succes'] = '0';
					$arr['error'] = $mysqlErrArr;
					echo json_encode($arr);
			}
			break;
			
			case "getdata":
			
			if(@$_GET['de_la_data']){
				$de_la_data = mysql_real_escape_string($_GET['de_la_data']);
			}else{
				$de_la_data = date("Y-m-d");
			}
			
			if(@$_GET['pana_la_data']){
				$pana_la_data = mysql_real_escape_string($_GET['pana_la_data']);
			}else{
				$pana_la_data = date("Y-m-d",strtotime(date("Y-m-d")) + 86400);
			}
			
			if(@$_GET['magnitudine']){
				$magnitudine = mysql_real_escape_string($_GET['magnitudine']);
			}else{
				$magnitudine = 3;
			}
			
			if(@$_GET['adancimeDeLa']){
				$adancimeDeLa = mysql_real_escape_string($_GET['adancimeDeLa']);
			}else{
				$adancimeDeLa = 45;
			}
			
			if(@$_GET['adancimePanaLa']){
				$adancimePanaLa = mysql_real_escape_string($_GET['adancimePanaLa']);
			}else{
				$adancimePanaLa = 150;
			}
			
			if(@$_GET['nord']){
				$nord = mysql_real_escape_string($_GET['nord']);
			}else{
				$nord = 90;
			}
			
			if(@$_GET['vest']){
				$vest = mysql_real_escape_string($_GET['vest']);
			}else{
				$vest = -180;
			}
			
			if(@$_GET['est']){
				$est = mysql_real_escape_string($_GET['est']);
			}else{
				$est = 180;
			}
			
			if(@$_GET['sud']){
				$sud = mysql_real_escape_string($_GET['sud']);
			}else{
				$sud = -90;
			}
			
		
			$res = mysql_query("select * from evenimente where (data_eveniment BETWEEN date('$de_la_data') AND date('$pana_la_data')) AND mag >= {$magnitudine} AND (adancime BETWEEN {$adancimeDeLa} AND {$adancimePanaLa}) AND (lat BETWEEN {$sud} AND {$nord})	AND (lon BETWEEN {$vest} AND {$est}) order by data_eveniment desc") or die(mysql_error());
			
			if(mysql_num_rows($res)==0){
				$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
				header('Content-Type: application/json');
				echo "callback(".json_encode($arr) .")";
			}else{
				$arr = array();
				while($row = mysql_fetch_array($res)){
					$arr[] =  $row;
				}
				header('Content-Type: application/json');
				echo "callback(".json_encode($arr) .")";
			}
			break;
			

			case "getstatiidupaeveniment":
			
			$id_eveniment = $_GET['eveniment'];
			$res = mysql_query("select * from evenimente_statii where id_eveniment in (". implode(',',$id_eveniment) . ")  ");
			
			if(mysql_num_rows($res)==0){
					$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
					header('Content-Type: application/json');
					echo "callback(".json_encode($arr) .")";
			}else{
				$arr = array();
				while($row = mysql_fetch_array($res)){
						$arr[] =  $row;
				}
				header('Content-Type: application/json');
				echo "callback(".json_encode($arr) .")";
			}
			
			
			/*
				$res = mysql_query("select * from statii where id_tara = 6");
				
				if(mysql_num_rows($res)==0){
					$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
					header('Content-Type: application/json');
					echo "callback(".json_encode($arr) .")";
				}else{
					$arr = array();
					$a = 0;
					while($row = mysql_fetch_array($res)){
						
						$arr[] =  $row;
						$id_statie = $row['id_statie'];
								$arrSenzori = array();
						$res_senzori = mysql_query("select * from senzori where id_statie = '$id_statie'");
						while($row_senzori = mysql_fetch_array($res_senzori)){
							$arrSenzori[] = $row_senzori['cod_senzor'];

						}
							$_['stream_'.$a] = implode(" ",$arrSenzori);
							
								
						$a++;
					}
					
					for($i=0;$i<mysql_num_rows($res);$i++){
						array_push($arr[$i],$_['stream_'.$i]);
					}	
					
					
					
					
					header('Content-Type: application/json');
					echo "callback(".json_encode($arr) .")";
				}
				*/
			break;


			case "getstatiibycod":
				$id_statie = mysql_real_escape_string($_GET['id_statie']);
				$res = mysql_query("select * from statii,tari where statii.id_statie = '$id_statie' and statii.id_tara = tari.id_tara");
				$canale  = $_GET['senzorilista'];

				if(mysql_num_rows($res)==0){
					$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
					header('Content-Type: application/json');
					echo "callback(".json_encode($arr) .")";
				}else{
					$arr = array();
					$a = 0;
					while($row = mysql_fetch_array($res)){
						$arr[] =  $row;
						
				
						/*
						$arr[] =  $row;
						$id_statie = $row['id_statie'];
						$arrSenzori = array();
						
					
						$res_senzori = mysql_query("select * from senzori where id_senzor in (". implode(',',$senzori) . ")   ");
						while($row_senzori = mysql_fetch_array($res_senzori)){
							$arrSenzori[] = $row_senzori['cod_senzor'];

						}
					
							$_['stream_'.$a] = implode(" ",$arrSenzori);
							
								
						$a++;
							*/
							
					}
					
					/*
					for($i=0;$i<mysql_num_rows($res);$i++){
						array_push($arr[$i],$_['stream_'.$i]);
					}	
					*/

					
					header('Content-Type: application/json');
					echo "callback(".json_encode($arr) .")";
					
				}	
				
			break;
			
			
			case "populeazaBycod":
					
					$res = mysql_query("select * from statii,tari where statii.id_tara = tari.id_tara ");
					if(mysql_num_rows($res)==0){
						$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
						header('Content-Type: application/json');
						echo "callback(".json_encode($arr) .")";
					}else{
						$arr = array();
						while($row = mysql_fetch_array($res)){
							$arr[] = $row;
						}
						header('Content-Type: application/json');
						echo "callback(".json_encode($arr) .")";
					}
			
			break;
			
			case "getsenzori":
					$id_statie = mysql_real_escape_string($_GET['id_statie']);
					$res = mysql_query("select * from statii where id_statie = '$id_statie' ");
					#$res = mysql_query("select * from senzori where id_statie = '$id_statie' ");
					if(mysql_num_rows($res)==0){
						$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
						header('Content-Type: application/json');
						echo "callback(".json_encode($arr) .")";
					}else{
					
						$arr = array();
						$row = mysql_fetch_array($res);
						$canale = $row['canal'];
						$arrCanale = explode(",",$canale);
						
						foreach($arrCanale as $canal){
							$arr[] = $canal;
						}
						
						header('Content-Type: application/json');
						echo "callback(".json_encode($arr) .")";
					}
			
			break;
			
			
			
			case "verificarequest":
					$email = mysql_real_escape_string($_GET['email']);
					$res = mysql_query("select * from requesturi where email_res = '$email' order by data_res desc limit 100 ");
					#$res = mysql_query("select * from senzori where id_statie = '$id_statie' ");
					if(mysql_num_rows($res)==0){
						$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
						header('Content-Type: application/json');
						echo "callback(".json_encode($arr) .")";
					}else{
					
						$arr = array();
						
						while($row = mysql_fetch_array($res)){
							$arr[] = $row;
						}
						
						header('Content-Type: application/json');
						echo "callback(".json_encode($arr) .")";
						#echo json_encode($arr);
						#echo '{"data":'.json_encode($arr).'}';
					}
			
			break;

			
			
			case "resqueststatii_absolute":
						
					$id_statieii = $_GET['id_statie'];
					$canale = $_GET['canale'];
					$allstation = $_GET['allstation'];
					$startdate = $_GET['startdate'];
					$enddate = $_GET['enddate'];
					$tipresq = $_GET['tipresq'];
					$tipfetch = $_GET['tipfetch'];
					$email = $_GET['email'];
					$fisierLST = date("Y-m-d_H-i") . "__" . $email . "_";		

					
					mysql_query("INSERT INTO requesturi (id_resquest,email_res,data_res,tip_res,tip_fetch) values (null,'$email',now(),'$tipresq','$tipfetch' )") or die(mysql_error());
					$mysql_last_id = mysql_insert_id();

					$arr = array("succes" => "1","error"=>"Requestul tau a fost trimis si este in progres. Vei primi un email cu linkul de unde il vei putea descarca.");
					echo json_encode($arr);

					
					switch($tipfetch){
					
						//FETCH TIP SCART
						case "scart":
								//SSH PROCES 
									$connection = ssh2_connect('91.212.254.9', 22);
									ssh2_auth_password($connection, 'sysop', 'sysop');
								//SSH PROCES 
							$listaArr = array();
							
							if($allstation == "da"){
								$res = mysql_query("select * from tari,statii where statii.id_tara = tari.id_tara");
								while($row = mysql_fetch_array($res)){
									$codTara = $row['cod_tara'];
									$codStatie = $row['cod_statie'];
								
									$listaArr[] = $startdate .";".$enddate.";".$codTara.".".$codStatie.".*.* \n";
									#echo $startdate .";".$enddate.";".$codTara.".".$codStatie.".*.* \n";
								}
								
								$listastring = implode(",",$listaArr);
								// COMANDA CREARE LISTA
									$stream = ssh2_exec($connection, "echo '". implode("",$listaArr)."' > '/home/sysop/eida/request/".$fisierLST.".lst'; echo '__COMANDA_TERMINATA_CREARE_LISTA_SCART__'");
								
									stream_set_blocking($stream, true);
									$data = '';
									while($buffer = fread($stream, 4096)) {
										$data .= $buffer;
									}
									fclose($stream);
								// COMANDA CREARE LISTA
									
									
									// COMANDA FECH MSSED
								if (strpos($data,"__COMANDA_TERMINATA_CREARE_LISTA_SCART__") !== false) {
										$stream_FETCH = ssh2_exec($connection, "seiscomp exec scart -ds --list '/home/sysop/eida/request/".$fisierLST.".lst' /home/sysop/seiscomp3/var/lib/archive > '/home/sysop/eida/mseed/".$fisierLST.".mseed'; echo '__COMANDA_SCART_TERMINATA__'");
	
									
										stream_set_blocking($stream_FETCH, true);
										$dataFECTH = '';
										while($buffer = fread($stream_FETCH, 4096)) {
											$dataFECTH .= $buffer;
										}
										fclose($stream_FETCH);
									
									
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									if (strpos($dataFECTH,"__COMANDA_SCART_TERMINATA__") !== false) {
											ssh2_scp_recv($connection, '/home/sysop/eida/mseed/'.$fisierLST.'.mseed', 'descarcari/'.$fisierLST.'.mseed');
											$to = $email;
											$subject = "Resquest " . $tipresq;
											$txt = "Link pentru descarcare: http://eida.infp.ro/descarcari/".$fisierLST.".mseed";
											$headers = "From: eida@infp.ro";
											mail($to,$subject,$txt,$headers);
											//, 
											mysql_query("update requesturi set status = 'terminat', list_res = '$listastring' where id_resquest = '$mysql_last_id' ");
									}
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									
								}
								// COMANDA FECH MSSED
								
								
								
								
							}else{
							
								foreach($id_statieii  as $key => $id_statie){
									// daca vrem sperat pe viitor canale $canale[$key];
									$res = mysql_query("select * from tari,statii where statii.id_statie = '$id_statie' and statii.id_tara = tari.id_tara");
									$row = mysql_fetch_array($res);
									$codTara = $row['cod_tara'];
									$codStatie = $row['cod_statie'];
									
									$listaArr[] = $startdate .";".$enddate.";".$codTara.".".$codStatie.".*.* \n";
									
								}
								$listastring = implode(",",$listaArr);
								// COMANDA CREARE LISTA
									$stream = ssh2_exec($connection, "echo '". implode("",$listaArr)."' > '/home/sysop/eida/request/".$fisierLST.".lst'; echo '__COMANDA_TERMINATA_CREARE_LISTA_SCART__'");
								
									stream_set_blocking($stream, true);
									$data = '';
									while($buffer = fread($stream, 4096)) {
										$data .= $buffer;
									}
									fclose($stream);
								// COMANDA CREARE LISTA
									
									
									
									// COMANDA FECH MSSED
								if (strpos($data,"__COMANDA_TERMINATA_CREARE_LISTA_SCART__") !== false) {
										$stream_FETCH = ssh2_exec($connection, "seiscomp exec scart -ds --list '/home/sysop/eida/request/".$fisierLST.".lst' /home/sysop/seiscomp3/var/lib/archive > '/home/sysop/eida/mseed/".$fisierLST.".mseed'; echo '__COMANDA_SCART_TERMINATA__'");
										
									
										stream_set_blocking($stream_FETCH, true);
										$dataFECTH = '';
										while($buffer = fread($stream_FETCH, 4096)) {
											$dataFECTH .= $buffer;
										}
										fclose($stream_FETCH);
									
									
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									if (strpos($dataFECTH,"__COMANDA_SCART_TERMINATA__") !== false) {
											ssh2_scp_recv($connection, '/home/sysop/eida/mseed/'.$fisierLST.'.mseed', 'descarcari/'.$fisierLST.'.mseed');
											$to = $email;
											$subject = "Resquest " . $tipresq;
											$txt = "Link pentru descarcare: http://eida.infp.ro/descarcari/".$fisierLST.".mseed";
											$headers = "From: eida@infp.ro";
											mail($to,$subject,$txt,$headers);
											//, 
											mysql_query("update requesturi set status = 'terminat', list_res = '$listastring' where id_resquest = '$mysql_last_id' ");
									}
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									
								}
								// COMANDA FECH MSSED
								
								
								
							}
						break;
						//FETCH TIP SCART
						
						//FETCH TIP ARCLINK
						case "arclink":
								//SSH PROCES 
									$connection = ssh2_connect('91.212.254.9', 22);
									ssh2_auth_password($connection, 'sysop', 'sysop');
								//SSH PROCES 
							$listaArr = array();
							
							if($allstation == "da"){
								//Toate statiile selectate pentru fetch
								$res = mysql_query("select * from tari,statii where statii.id_tara = tari.id_tara");
								while($row = mysql_fetch_array($res)){
									$codTara = $row['cod_tara'];
									$codStatie = $row['cod_statie'];
									
									$dataST = date("Y,m,d,H,i",strtotime($startdate));
									$dataFN = date("Y,m,d,H,i",strtotime($enddate));
									
									//2014,05,28,10,00,00 2014,05,28,10,20,00 RO ARR *
									
									$listaArr[] = $dataST ." ".$dataFN." ".$codTara." ".$codStatie." *\n";
									#echo $dataST ." ".$dataFN." ".$codTara." ".$codStatie." * \n";
								}
								
							}else{
								//Doar statiile specificate
								foreach($id_statieii  as $key => $id_statie){
									// daca vrem sperat pe viitor canale $canale[$key];
									$res = mysql_query("select * from tari,statii where statii.id_statie = '$id_statie' and statii.id_tara = tari.id_tara");
									$row = mysql_fetch_array($res);
									$codTara = $row['cod_tara'];
									$codStatie = $row['cod_statie'];
									
									$dataST = date("Y,m,d,H,i",strtotime($startdate));
									$dataFN = date("Y,m,d,H,i",strtotime($enddate));
									
									//2014,05,28,10,00,00 2014,05,28,10,20,00 RO ARR *
									
									$listaArr[] = $dataST ." ".$dataFN." ".$codTara." ".$codStatie." *\n";
									#echo $dataST ." ".$dataFN." ".$codTara." ".$codStatie." * \n";

								}
							}
							
							$listastring = implode(",",$listaArr);
								// COMANDA CREARE LISTA
									$stream = ssh2_exec($connection, "echo '". implode("",$listaArr)."' > '/home/sysop/eida/request/".$fisierLST.".txt'; echo '__COMANDA_TERMINATA_CREARE_LISTA_SCART__'");
								
								
									stream_set_blocking($stream, true);
									$data = '';
									while($buffer = fread($stream, 4096)) {
										$data .= $buffer;
									}
									fclose($stream);
								// COMANDA CREARE LISTA
								
								
								
								
								if (strpos($data,"__COMANDA_TERMINATA_CREARE_LISTA_SCART__") !== false) {
										$stream_FETCH = ssh2_exec($connection, "seiscomp exec arclink_fetch -t 3600 -u sysop -o '/home/sysop/eida/mseed/".$fisierLST.".mseed' '/home/sysop/eida/request/".$fisierLST.".txt'; echo '__COMANDA_SCART_TERMINATA__'");
									
									
										stream_set_blocking($stream_FETCH, true);
										$dataFECTH = '';
										while($buffer = fread($stream_FETCH, 4096)) {
											$dataFECTH .= $buffer;
										}
										fclose($stream_FETCH);
									
									
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									if (strpos($dataFECTH,"__COMANDA_SCART_TERMINATA__") !== false) {
											ssh2_scp_recv($connection, '/home/sysop/eida/mseed/'.$fisierLST.'.mseed', 'descarcari/'.$fisierLST.'.mseed');
											$to = $email;
											$subject = "Resquest " . $tipresq;
											$txt = "Link pentru descarcare: http://eida.infp.ro/descarcari/".$fisierLST.".mseed";
											$headers = "From: eida@infp.ro";
											mail($to,$subject,$txt,$headers);
											//, 
											mysql_query("update requesturi set status = 'terminat', list_res = '$listastring' where id_resquest = '$mysql_last_id' ");
									}
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									
								}	
							
							
						break;
						//FETCH TIP ARCLINK
						
					}

				

					

					
		



					
					
					
					/*
					


					if(mysql_num_rows($res)==0){
						$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
						header('Content-Type: application/json');
						echo "callback(".json_encode($arr) .")";
					}else{
						$arr = array();
						while($row = mysql_fetch_array($res)){
							$arr[] = $row;
						}
						

						
						header('Content-Type: application/json');
						echo "callback(".json_encode($arr) .")";
					}
					*/
					
					
			break;
			
			
			
			case "resqueststatii_relative":
						
					$id_statieii = $_GET['id_statie'];
					$evenimente = $_GET['evenimente'];
					$canale = $_GET['canale'];
					$allstation = $_GET['allstation'];
					$minutebefore = $_GET['minutebefore'];
					$minuteafter = $_GET['minuteafter'];
					$tipresq = $_GET['tipresq'];
					$tipfetch = $_GET['tipfetch'];
					$email = $_GET['email'];
					$fisierLST = date("Y-m-d_H-i") . "__" . $email . "_";	

				
					
					$res_eveniment = mysql_query("select * from evenimente where id_cutremur = '$evenimente[0]'") or die(mysql_error());
					$row_eveniment = mysql_fetch_array($res_eveniment);
					$data_eve_start = date("Y-m-d H:i:s",strtotime($row_eveniment['data_eveniment']) - (10800 - ($minutebefore * 60)) ); 
					$data_eve_end = date("Y-m-d H:i:s",strtotime($row_eveniment['data_eveniment']) - (10800 - ($minuteafter * 60)));

					
					#echo "dI " . $data_eve_start . " DF "  .$data_eve_end;
					
					mysql_query("INSERT INTO requesturi (id_resquest,email_res,data_res,tip_res,tip_fetch) values (null,'$email',now(),'$tipresq','$tipfetch' )") or die(mysql_error());
					$mysql_last_id = mysql_insert_id();

					#$arr = array("succes" => "1","error"=>"Requestul ".$row_eveniment['data_eveniment']." tau a fost trimis si este in progres. Vei primi un email cu linkul de unde il vei putea descarca.");
					#echo json_encode($arr);

					
					switch($tipfetch){
					
						//FETCH TIP SCART
						case "scart":
								//SSH PROCES 
									$connection = ssh2_connect('91.212.254.9', 22);
									ssh2_auth_password($connection, 'sysop', 'sysop');
								//SSH PROCES 
							$listaArr = array();
							
							if($allstation == "da"){
								$res = mysql_query("select * from tari,statii where statii.id_tara = tari.id_tara");
								while($row = mysql_fetch_array($res)){
									$codTara = $row['cod_tara'];
									$codStatie = $row['cod_statie'];
								
									$listaArr[] = $data_eve_start .";".$data_eve_end.";".$codTara.".".$codStatie.".*.* \n";
									#echo $startdate .";".$enddate.";".$codTara.".".$codStatie.".*.* \n";
								}
								
								$listastring = implode(",",$listaArr);
								// COMANDA CREARE LISTA

									$stream = ssh2_exec($connection, "echo '". implode("",$listaArr)."' > '/home/sysop/eida/request/".$fisierLST.".lst'; echo '__COMANDA_TERMINATA_CREARE_LISTA_SCART__'");
								
									stream_set_blocking($stream, true);
									$data = '';
									while($buffer = fread($stream, 4096)) {
										$data .= $buffer;
									}
									fclose($stream);
								// COMANDA CREARE LISTA
									
									
									// COMANDA FECH MSSED
								if (strpos($data,"__COMANDA_TERMINATA_CREARE_LISTA_SCART__") !== false) {
										$stream_FETCH = ssh2_exec($connection, "seiscomp exec scart -ds --list '/home/sysop/eida/request/".$fisierLST.".lst' /home/sysop/seiscomp3/var/lib/archive > '/home/sysop/eida/mseed/".$fisierLST.".mseed'; echo '__COMANDA_SCART_TERMINATA__'");
									
										stream_set_blocking($stream_FETCH, true);
										$dataFECTH = '';
										while($buffer = fread($stream_FETCH, 4096)) {
											$dataFECTH .= $buffer;
										}
										fclose($stream_FETCH);
									
									
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									if (strpos($dataFECTH,"__COMANDA_SCART_TERMINATA__") !== false) {
											ssh2_scp_recv($connection, '/home/sysop/eida/mseed/'.$fisierLST.'.mseed', 'descarcari/'.$fisierLST.'.mseed');
											$to = $email;
											$subject = "Resquest " . $tipresq;
											$txt = "Link pentru descarcare: http://eida.infp.ro/descarcari/".$fisierLST.".mseed";
											$headers = "From: eida@infp.ro";
											mail($to,$subject,$txt,$headers);
											//, 
											mysql_query("update requesturi set status = 'terminat', list_res = '$listastring' where id_resquest = '$mysql_last_id' ");
									}
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									
								}
								// COMANDA FECH MSSED
								
								
								
							}else{
							
								foreach($id_statieii  as $key => $id_statie){
									// daca vrem sperat pe viitor canale $canale[$key];
									$res = mysql_query("select * from tari,statii where statii.id_statie = '$id_statie' and statii.id_tara = tari.id_tara");
									$row = mysql_fetch_array($res);
									$codTara = $row['cod_tara'];
									$codStatie = $row['cod_statie'];
									
									$listaArr[] = $data_eve_start .";".$data_eve_end.";".$codTara.".".$codStatie.".*.* \n";
									
								}
								$listastring = implode(",",$listaArr);
								// COMANDA CREARE LISTA
									$stream = ssh2_exec($connection, "echo '". implode("",$listaArr)."' > '/home/sysop/eida/request/".$fisierLST.".lst'; echo '__COMANDA_TERMINATA_CREARE_LISTA_SCART__'");
								
									stream_set_blocking($stream, true);
									$data = '';
									while($buffer = fread($stream, 4096)) {
										$data .= $buffer;
									}
									fclose($stream);
								// COMANDA CREARE LISTA
									
									
									// COMANDA FECH MSSED
								if (strpos($data,"__COMANDA_TERMINATA_CREARE_LISTA_SCART__") !== false) {
										$stream_FETCH = ssh2_exec($connection, "seiscomp exec scart -ds --list '/home/sysop/eida/request/".$fisierLST.".lst' /home/sysop/seiscomp3/var/lib/archive > '/home/sysop/eida/mseed/".$fisierLST.".mseed'; echo '__COMANDA_SCART_TERMINATA__'");
									
										stream_set_blocking($stream_FETCH, true);
										$dataFECTH = '';
										while($buffer = fread($stream_FETCH, 4096)) {
											$dataFECTH .= $buffer;
										}
										fclose($stream_FETCH);
									
									
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									if (strpos($dataFECTH,"__COMANDA_SCART_TERMINATA__") !== false) {
											ssh2_scp_recv($connection, '/home/sysop/eida/mseed/'.$fisierLST.'.mseed', 'descarcari/'.$fisierLST.'.mseed');
											$to = $email;
											$subject = "Resquest " . $tipresq;
											$txt = "Link pentru descarcare: http://eida.infp.ro/descarcari/".$fisierLST.".mseed";
											$headers = "From: eida@infp.ro";
											mail($to,$subject,$txt,$headers);
											//, 
											mysql_query("update requesturi set status = 'terminat', list_res = '$listastring' where id_resquest = '$mysql_last_id' ");
									}
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									
								}
								// COMANDA FECH MSSED
								
								
								
							}
						break;
						//FETCH TIP SCART
						
						//FETCH TIP ARCLINK
						case "arclink":
								//SSH PROCES 
									$connection = ssh2_connect('91.212.254.9', 22);
									ssh2_auth_password($connection, 'sysop', 'sysop');
								//SSH PROCES 
							$listaArr = array();
							
							if($allstation == "da"){
								//Toate statiile selectate pentru fetch
								$res = mysql_query("select * from tari,statii where statii.id_tara = tari.id_tara");
								while($row = mysql_fetch_array($res)){
									$codTara = $row['cod_tara'];
									$codStatie = $row['cod_statie'];
									
									$dataST = date("Y,m,d,H,i",strtotime($data_eve_start));
									$dataFN = date("Y,m,d,H,i",strtotime($data_eve_end));
									
									//2014,05,28,10,00,00 2014,05,28,10,20,00 RO ARR *
									
									$listaArr[] = $dataST ." ".$dataFN." ".$codTara." ".$codStatie." *\n";
									#echo $dataST ." ".$dataFN." ".$codTara." ".$codStatie." * \n";
								}
								
							}else{
								//Doar statiile specificate
								foreach($id_statieii  as $key => $id_statie){
									// daca vrem sperat pe viitor canale $canale[$key];
									$res = mysql_query("select * from tari,statii where statii.id_statie = '$id_statie' and statii.id_tara = tari.id_tara");
									$row = mysql_fetch_array($res);
									$codTara = $row['cod_tara'];
									$codStatie = $row['cod_statie'];
									
									$dataST = date("Y,m,d,H,i",strtotime($data_eve_start));
									$dataFN = date("Y,m,d,H,i",strtotime($data_eve_end));
									
									//2014,05,28,10,00,00 2014,05,28,10,20,00 RO ARR *
									
									$listaArr[] = $dataST ." ".$dataFN." ".$codTara." ".$codStatie." *\n";
									#echo $dataST ." ".$dataFN." ".$codTara." ".$codStatie." * \n";

								}
							}
							
							$listastring = implode(",",$listaArr);
								// COMANDA CREARE LISTA
									$stream = ssh2_exec($connection, "echo '". implode("",$listaArr)."' > '/home/sysop/eida/request/".$fisierLST.".txt'; echo '__COMANDA_TERMINATA_CREARE_LISTA_SCART__'");
								
									stream_set_blocking($stream, true);
									$data = '';
									while($buffer = fread($stream, 4096)) {
										$data .= $buffer;
									}
									fclose($stream);
								// COMANDA CREARE LISTA
								
								
								
								
								if (strpos($data,"__COMANDA_TERMINATA_CREARE_LISTA_SCART__") !== false) {
										$stream_FETCH = ssh2_exec($connection, "seiscomp exec arclink_fetch -t 3600 -u sysop -o '/home/sysop/eida/mseed/".$fisierLST.".mseed' '/home/sysop/eida/request/".$fisierLST.".txt'; echo '__COMANDA_SCART_TERMINATA__'");
									
										stream_set_blocking($stream_FETCH, true);
										$dataFECTH = '';
										while($buffer = fread($stream_FETCH, 4096)) {
											$dataFECTH .= $buffer;
										}
										fclose($stream_FETCH);
									
									
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									if (strpos($dataFECTH,"__COMANDA_SCART_TERMINATA__") !== false) {
											ssh2_scp_recv($connection, '/home/sysop/eida/mseed/'.$fisierLST.'.mseed', 'descarcari/'.$fisierLST.'.mseed');
											$to = $email;
											$subject = "Resquest " . $tipresq;
											$txt = "Link pentru descarcare: http://eida.infp.ro/descarcari/".$fisierLST.".mseed";
											$headers = "From: eida@infp.ro";
											mail($to,$subject,$txt,$headers);
											//, 
											mysql_query("update requesturi set status = 'terminat', list_res = '$listastring' where id_resquest = '$mysql_last_id' ");
									}
									// MUTARE FISIER DIN SERVER CATRE WWW SERVER
									
								}	
							
							
						break;
						//FETCH TIP ARCLINK
						
					}
					
			break;
			
			case "test":
					$evenimente  = 67;
					$minutebefore = -5;
					$minuteafter = 5;
					$res_eveniment = mysql_query("select * from evenimente where id_cutremur = '$evenimente'");
					$row_eveniment = mysql_fetch_array($res_eveniment);
					$data_eve_start = date("Y-m-d H:i:s",strtotime($row_eveniment['data_eveniment']) - (10800 - ($minutebefore * 60)) ); 
					$data_eve_end = date("Y-m-d H:i:s",strtotime($row_eveniment['data_eveniment']) - (10800 - ($minuteafter * 60)));

					echo "dI " . $data_eve_start . " DF "  .$data_eve_end;
			break;
			
			case "evenimenteall":
			
			if(@$_GET['de_la_data']){
				$de_la_data = mysql_real_escape_string($_GET['de_la_data']);
			}else{
				$de_la_data = date("Y-m-d");
			}
			
			if(@$_GET['nord']){
				$nord = mysql_real_escape_string($_GET['nord']);
			}else{
				$nord = 90;
			}
			
			if(@$_GET['vest']){
				$vest = mysql_real_escape_string($_GET['vest']);
			}else{
				$vest = -180;
			}
			
			if(@$_GET['est']){
				$est = mysql_real_escape_string($_GET['est']);
			}else{
				$est = 180;
			}
			
			if(@$_GET['sud']){
				$sud = mysql_real_escape_string($_GET['sud']);
			}else{
				$sud = -90;
			}
			
			if(@$_GET['magnitudine']){
				$magnitudine = mysql_real_escape_string($_GET['magnitudine']);
			}else{
				$magnitudine = 3;
			}
			
			$pana_la_data = date("Y-m-d",strtotime(date("Y-m-d")) + 86400);
			
			$res = mysql_query("select * from evenimente where (data_eveniment BETWEEN date('$de_la_data') AND date('$pana_la_data')) AND mag >= {$magnitudine} AND (lat BETWEEN {$sud} AND {$nord})	AND (lon BETWEEN {$vest} AND {$est}) order by data_eveniment desc") or die(mysql_error());
			
			if(mysql_num_rows($res)==0){
				$arr = array("succes" => "0","error"=>$_['eroare_cautare_evenimente']);
				header('Content-Type: application/json');
				echo json_encode($arr);
			}else{
				$arr = array();
				while($row = mysql_fetch_array($res)){
					$arr[] =  $row;
				}
				header('Content-Type: application/json');
				echo json_encode($arr);
			}
			
			break;
			
			
			default:
					$arr = array("succes" => "0","error"=>"Actiune irelevanta");
					echo json_encode($arr);
					header('HTTP/1.1 401 Unauthorized', true, 401);
			break;
		}
	}
	
	
	
	
	














?>